﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using BasePlatformNetAgent;

namespace BasePlatformNetIO
{
	public class BasePlatformIO
	{
		private string ioVersion = "v1.0.4 @ 2019.1.8";

		private int iBlueCommandNum = 0;
		private int ledDebugCommandNum = 0;

		private string basePipeName = @"\\.\pipe\basePipe";
		private string ibluePipeName = @"\\.\pipe\ibluePipe";
		private string ibluePipeName_Loop = @"\\.\pipe\ibluePipeLoop";

		private string baseJsonPipeName = @"\\.\pipe\basePipeJson";
		private string ledDebugPipeName = @"\\.\pipe\ledDebugPipe";

		private bool hasRegistIBluePipe = false;
		private bool hasRegistLedDebugPipe = false;

		private bool lastSubEquActiveState = false;

		// 内部函数 - 计算新的 iBlue 命令编号
		private void calcNewIBlueCommandNum()
		{
			iBlueCommandNum++;

			if (iBlueCommandNum > int.MaxValue - 10)
			{
				iBlueCommandNum = 1;
			}
		}

		// 内部函数 - 计算新的 LedDebug 命令编号
		private void calcNewLedDebugCommandNum()
		{
			ledDebugCommandNum++;

			if (ledDebugCommandNum > int.MaxValue - 10)
			{
				ledDebugCommandNum = 1;
			}
		}

		// iBluePlayer 命令前处理
		private ShareMessage preTreatIBlue(bool calcCommandNum, ShareMessage response)
		{
			if (calcCommandNum)
			{
				calcNewIBlueCommandNum();
			}

			if (hasRegistIBluePipe == false)
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "iBlue Pipe has not registed";
			}
			else
			{
				response = null;
			}

			return response;
		}

		// LedDebug 命令前处理
		private ShareMessage preTreatLedDebug(bool calcCommandNum, ShareMessage response)
		{
			if (calcCommandNum)
			{
				calcNewLedDebugCommandNum();
			}

			if (hasRegistLedDebugPipe == false)
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "LedDebug Pipe has not registed";
			}
			else
			{
				response = null;
			}

			return response;
		}

		// 命令后处理（查询接口不执行命令后处理）
		private ShareMessage postTreat(ShareMessage response)
		{
			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "pipe error: response == null";
			}

			return response;
		}

		//【基础】获取commandNum
		public int getIBlueCommandNum()
		{
			return iBlueCommandNum;
		}

		//【基础】获取commandNum
		public int getLedDebugCommandNum()
		{
			return ledDebugCommandNum;
		}

		//【基础】注册管道
		public string registPipe(string moduleName)
		{
			if (moduleName == null || !moduleName.ToLower().Equals("iblue") && !moduleName.ToLower().Equals("leddebug"))
			{
				return "wrong moduleName";
			}
			else if (moduleName.ToLower().Equals("iblue"))
			{
				calcNewIBlueCommandNum();
			}
			else if (moduleName.ToLower().Equals("leddebug"))
			{
				calcNewLedDebugCommandNum();
			}

			ShareMessage registMsg = new ShareMessage();
			registMsg.Type = MessageType.BaseMessage;
			registMsg.Value = MessageValue.Base_Regist;
			registMsg.Parameters = new string[] { moduleName };

			ShareMessage response = null;
			if (moduleName.ToLower().Equals("iblue"))
			{
				NamedPipeNative.SendMsg(basePipeName, registMsg, out response);
			}
			else if (moduleName.ToLower().Equals("leddebug"))
			{
				NamedPipeNative.SendMsg(baseJsonPipeName, registMsg, out response, true);
			}

			if (response != null)
			{
				if (response.Handled && moduleName.ToLower().Equals("iblue"))
				{
					hasRegistIBluePipe = true;
				}
				else if (response.Handled && moduleName.ToLower().Equals("leddebug"))
				{
					hasRegistLedDebugPipe = true;
				}

				return response.Value + "&WinPcap=" + response.PayloadStr;
			}
			else
			{
				return "noResponse";
			}
		}

		//【基础】取消注册管道
		public string unregistPipe(string moduleName)
		{
			if (moduleName == null || !moduleName.ToLower().Equals("iblue") && !moduleName.ToLower().Equals("leddebug"))
			{
				return "wrong moduleName";
			}

			if (moduleName.ToLower().Equals("iblue"))
			{
				calcNewIBlueCommandNum();
			}
			else if (moduleName.ToLower().Equals("leddebug"))
			{
				calcNewLedDebugCommandNum();
			}

			if (moduleName.ToLower().Equals("iblue") && hasRegistIBluePipe == false)
			{
				return "No need unRegist iBlue Pipe";
			}
			if (moduleName.ToLower().Equals("leddebug") && hasRegistLedDebugPipe == false)
			{
				return "No need unRegist LedDebug Pipe";
			}

			ShareMessage unregistMsg = new ShareMessage();
			unregistMsg.Type = MessageType.BaseMessage;
			unregistMsg.Value = MessageValue.Base_UnRegist;
			unregistMsg.Parameters = new string[] { moduleName };

			ShareMessage responseMsg = new ShareMessage();
			if (moduleName.ToLower().Equals("iblue"))
			{
				NamedPipeNative.SendMsg(basePipeName, unregistMsg, out responseMsg);
			}
			else if (moduleName.ToLower().Equals("leddebug"))
			{
				NamedPipeNative.SendMsg(baseJsonPipeName, unregistMsg, out responseMsg, true);
			}

			if (responseMsg != null)
			{
				return responseMsg.Value;
			}
			else
			{
				return "noResponse";
			}
		}

		#region iBluePlayer

		//【测试】发送 iBlue 测试消息
		public ShareMessage sendIBlueMessage(string value)
		{
			ShareMessage response = preTreatIBlue(false, null);

			ShareMessage sendMsg = new ShareMessage();
			sendMsg.Type = MessageType.IBlueMessage;
			sendMsg.Value = MessageValue.IBlue_Test;
			sendMsg.Parameters = new string[] { value };

			NamedPipeNative.SendMsg(ibluePipeName, sendMsg, out response);

			response = postTreat(response);

			return response;
		}

        //【命令】开始下发显示数据, 默认帧频fps=30
		public ShareMessage startSendDispData(int x, int y, int width, int height)
		{
			return startSendDispData(x, y, width, height, 30);
		}

		//【命令】开始下发显示数据
		public ShareMessage startSendDispData(int x, int y, int width, int height, int fps)
		{
			ShareMessage response = preTreatIBlue(true, null);

			if (x < 0 || y < 0 || width < 0 || height < 0 || fps <= 0)
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "wrong parameters: x = " + x + ", y = " + y + ", width = " + width + ", height = " + height + ", fps = " + fps;
				return response;
			}

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_StartSendDispData;
			commandMsg.Parameters = new string[] { x.ToString(), y.ToString(), width.ToString(), height.ToString(), fps.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】停止下发显示数据
		public ShareMessage stopSendDispData()
		{
			ShareMessage response = preTreatIBlue(true, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_StopSendDispData;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】暂停下发显示数据
		public ShareMessage pauseSendDispData()
		{
			ShareMessage response = preTreatIBlue(true, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Id = iBlueCommandNum.ToString();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_PauseSendDispData;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】恢复下发显示数据
		public ShareMessage resumeSendDispData()
		{
			ShareMessage response = preTreatIBlue(true, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_ResumeSendDispData;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】改变截屏区域位置
		public ShareMessage changeCaptureArea(int x, int y, int width, int height)
		{
			ShareMessage response = preTreatIBlue(true, null);

			if (x < 0 || y < 0 || width < 0 || height < 0)
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "wrong parameters: x = " + x + ", y = " + y + ", width = " + width + ", height = " + height;
				return response;
			}

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_ChangeCaptureArea;
			commandMsg.Parameters = new string[] { x.ToString(), y.ToString(), width.ToString(), height.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】改变截屏帧频
		public ShareMessage changeCaptureFps(int fps)
		{
			ShareMessage response = preTreatIBlue(true, null);

			if (fps < 0 || fps > 60)
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "wrong parameters: fps should be in [0,60]";
				return response;
			}

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_ChangeCaptureFps;
			commandMsg.Parameters = new string[] { fps.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】从 lwf 布线文件中读取 bin 区读取数据 / 从 Json 文件中读取数据
		public ShareMessage readBinDataFromLwfOrJson(string dataFilePath, bool useJson)
		{
			ShareMessage response = preTreatIBlue(true, null);

			if (dataFilePath == null || dataFilePath.Trim().Equals(""))
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "wrong parameters: dataFilePath = null";
				return response;
			}

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_ReadBinDataFromLwfOrJson;
			if (useJson == false)
			{
				commandMsg.Parameters = new string[] { dataFilePath.ToString(), "lwf" };
			}
			else
			{
				commandMsg.Parameters = new string[] { dataFilePath.ToString(), "json" };
			}

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】设置异形映射使能, 更新异形地址映射表
		public ShareMessage setAddrMapEnable(bool enable)
		{
			ShareMessage response = preTreatIBlue(true, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_SetAddrMapEnable;
			commandMsg.Parameters = new string[] { enable.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】关闭 BasePlatformAgent
		public ShareMessage closeBasePlatformAgent()
		{
			ShareMessage response = preTreatIBlue(true, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_CloseAgent;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】设置是否支持交换机
		public ShareMessage setUseSwitch(bool useSwitch)
		{
			ShareMessage response = preTreatIBlue(true, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_SetUseSwitch;
			commandMsg.Parameters = new string[] { useSwitch.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】读取交换机配置文件
		public ShareMessage readSwitchConfigFile(string configFilePath)
		{
			ShareMessage response = preTreatIBlue(true, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_ReadSwitchConfigFile;
			commandMsg.Parameters = new string[] { configFilePath.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】显示硬件设置界面
		public ShareMessage showHardConfigForm()
		{
			ShareMessage response = preTreatIBlue(true, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_ShowHardConfigForm;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】设置 iBluePlayer 的播放状态
		public ShareMessage setIBluePlayingState(bool playing)
		{
			ShareMessage response = preTreatIBlue(true, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_SetPlayingState;
			commandMsg.Parameters = new string[] { playing.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】设置节目亮度
		public ShareMessage setProgramBrightness(int bright)
		{
			ShareMessage response = preTreatIBlue(true, null);

			if (bright < 0 || bright > 255)
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "wrong parameters: bright should be in [0,255]";
				return response;
			}

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_SetProgramBrightness;
			commandMsg.Parameters = new string[] { bright.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

        //【查询】查询底层硬件设备的连接状态 - 底层支持命令, 周期执行, 不更新命令编号
        public ShareMessage getSubEquActiveState()
        {
			ShareMessage response = preTreatIBlue(false, null);

            ShareMessage commandMsg = new ShareMessage();
            commandMsg.Type = MessageType.IBlueMessage;
            commandMsg.Value = MessageValue.IBlue_GetSubEquActiveState;

            NamedPipeNative.SendMsg(ibluePipeName_Loop, commandMsg, out response);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = lastSubEquActiveState;
				response.Value = "success";
			}
			else
			{
				if (response.Parameters[0].Equals("true"))
				{
					lastSubEquActiveState = true;
				}
				else
				{
					lastSubEquActiveState = false;
				}
			}

            // 不执行命令后处理

            return response;
        }

		//【查询】获取异形映射参数 - addrMapEnable, addrSrcFileType, addrSrcFilePath
		public ShareMessage getAddrMapParameter()
		{
			ShareMessage response = preTreatIBlue(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_GetAddrMapParameter;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = true;
				response.Value = "success";
				response.Parameters = new string[3];
				response.Parameters[0] = "false";
				response.Parameters[1] = "";
				response.Parameters[2] = "";
				response.Parameters[3] = "0";
				response.Parameters[4] = "0";
			}
			else
			{
			}

			// 不执行命令后处理

			return response;
		}

		//【查询】获取异形映射表
		public ShareMessage getAddrMapTable(string type, ref int[] data)
		{
			ShareMessage response = preTreatIBlue(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_GetAddrMapTable;
			commandMsg.Parameters = new string[] { type };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = true;
				response.Value = "error";
			}
			else
			{
				if (response.PayloadData != null && response.PayloadData.Length > 0)
				{
					data = new int[response.PayloadData.Length];
					Array.Copy(response.PayloadData, data, data.Length);
				}
			}

			// 不执行命令后处理

			return response;
		}

		//【查询】获取 Agent 版本
		public ShareMessage getAgentVersion()
		{
			ShareMessage response = preTreatIBlue(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_GetAgentVersion;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = true;
				response.Value = "error";
			}
			else
			{
				// response 中的版本信息已由 Agent 设置
			}

			// 不执行命令后处理

			return response;
		}

		//【查询】获取底层设备链信息（Json格式）
		public ShareMessage getDeviceChainJson()
		{
			ShareMessage response = preTreatIBlue(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_GetDeviceChainJson;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = true;
				response.Value = "error";
			}

			// 不执行命令后处理

			return response;
		}

		//【查询】获取底层设备链摘要信息（Json格式）
		public ShareMessage getDeviceChainStatusJson()
		{
			ShareMessage response = preTreatIBlue(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_GetDeviceChainStatusJson;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = true;
				response.Value = "error";
			}

			// 不执行命令后处理

			return response;
		}

		//【查询】获取底层设备链详细信息（Json格式）
		public ShareMessage getDeviceChainDetailJson()
		{
			ShareMessage response = preTreatIBlue(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_GetDeviceChainDetailJson;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = true;
				response.Value = "error";
			}

			// 不执行命令后处理

			return response;
		}

		//【查询】获取底层硬件配置窗展开状态
		public ShareMessage getConfigExpandState()
		{
			ShareMessage response = preTreatIBlue(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_GetConfigExpandState;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = true;
				response.Value = "error";
			}

			// 不执行命令后处理

			return response;
		}

		//【查询】获取分控配置 sub 数据
		public ShareMessage getSubConfigData(ref byte[] data)
		{
			ShareMessage response = preTreatIBlue(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_GetSubConfigData;

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = true;
				response.Value = "error";
			}
			else
			{
				if (response.PayloadBytes != null && response.PayloadBytes.Length > 0)
				{
					if (data == null || data.Length != 128 * 1024)
					{
						data = new byte[128 * 1024];
					}

					Array.Copy(response.PayloadBytes, data, data.Length);
				}
			}

			// 不执行命令后处理

			return response;
		}

		//【命令】设置 Mcu 显示参数
		public ShareMessage setMcuDispParameter(int brightTotal, int brightR, int brightG, int brightB, int brightW, int gamma)
		{
			ShareMessage response = preTreatIBlue(true, null);

			if ((brightTotal < 0 || brightTotal > 255) || (brightR < 0 || brightR > 255) || (brightG < 0 || brightG > 255)
				|| (brightB < 0 || brightB > 255) || (brightW < 0 || brightW > 255) || (gamma < 0 || gamma > 255))
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "wrong parameters";
				return response;
			}

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_SetMcuDispParameter;
			commandMsg.Parameters = new string[] { brightTotal.ToString(), brightR.ToString(), brightG.ToString(), brightB.ToString(), brightW.ToString(), gamma.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】设置用户颜色模式
		public ShareMessage setUserColorType(int userColorType)
		{
			ShareMessage response = preTreatIBlue(true, null);

			if (userColorType < 0 || userColorType > 255)
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "wrong parameters: userColorType should be in [0,255]";
				return response;
			}

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_SetUserColorType;
			commandMsg.Parameters = new string[] { userColorType.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

		//【命令】设置用户语言
		public ShareMessage setUserLanguage(int userLanguage)
		{
			ShareMessage response = preTreatIBlue(true, null);

			if (userLanguage < 0 || userLanguage > 2)
			{
				response = new ShareMessage();
				response.Handled = false;
				response.Value = "wrong parameters: userLanguage should be in [0,2]";
				return response;
			}

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_SetUserLanguage;
			commandMsg.Parameters = new string[] { userLanguage.ToString() };

			NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

			response = postTreat(response);

			return response;
		}

        //【命令】发起ArtNet设备链查询
        public ShareMessage startQueryArtDeviceChain() {
            ShareMessage response = preTreatIBlue(true, null);

            ShareMessage commandMsg = new ShareMessage();
            commandMsg.Type = MessageType.IBlueMessage;
            commandMsg.Value = MessageValue.IBlue_StartQueryArtDeviceChain;

            NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

            response = postTreat(response);

            return response;
        }

        //【查询】获取ArtNet基准设备链信息
        public ShareMessage getBaseArtDeviceChain() {
            ShareMessage response = preTreatIBlue(false, null);

            ShareMessage commandMsg = new ShareMessage();
            commandMsg.Type = MessageType.IBlueMessage;
            commandMsg.Value = MessageValue.IBlue_GetBaseArtDeviceChain;

            NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

            if (response == null) {
                response = new ShareMessage();
                response.Handled = true;
                response.Value = "error";
            }

            // 不执行命令后处理

            return response;
        }

        //【查询】获取ArtNet当前设备链信息
        public ShareMessage getCurrentArtDeviceChain() {
            ShareMessage response = preTreatIBlue(false, null);

            ShareMessage commandMsg = new ShareMessage();
            commandMsg.Type = MessageType.IBlueMessage;
            commandMsg.Value = MessageValue.IBlue_GetCurrentArtDeviceChain;

            NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

            if (response == null) {
                response = new ShareMessage();
                response.Handled = true;
                response.Value = "error";
            }

            // 不执行命令后处理

            return response;
        }

        //【命令】设置ArtNet当前设备链信息设为基准
        public ShareMessage setCurrentArtDeviceChainToBase() {
            ShareMessage response = preTreatIBlue(true, null);

            ShareMessage commandMsg = new ShareMessage();
            commandMsg.Type = MessageType.IBlueMessage;
            commandMsg.Value = MessageValue.IBlue_StartQueryArtDeviceChain;

            NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

            response = postTreat(response);

            return response;
        }

        //【查询】获取ArtNet错误信息
        public ShareMessage getErrorArtDevice() {
            ShareMessage response = preTreatIBlue(false, null);

            ShareMessage commandMsg = new ShareMessage();
            commandMsg.Type = MessageType.IBlueMessage;
            commandMsg.Value = MessageValue.IBlue_GetErrorArtDevice;

            NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

            if (response == null) {
                response = new ShareMessage();
                response.Handled = true;
                response.Value = "error";
            }

            // 不执行命令后处理

            return response;
        }

        //【查询】获取ArtNet灯具详情
        public ShareMessage getArtDeviceDetail(string uid) {
            ShareMessage response = preTreatIBlue(false, null);

            ShareMessage commandMsg = new ShareMessage();
            commandMsg.Type = MessageType.IBlueMessage;
            commandMsg.Value = MessageValue.IBlue_GetArtDeviceDetail;
            commandMsg.Parameters = new string[] { uid };

            NamedPipeNative.SendMsg(ibluePipeName, commandMsg, out response);

            if (response == null) {
                response = new ShareMessage();
                response.Handled = true;
                response.Value = "error";
            }

            // 不执行命令后处理

            return response;
        } 

		#endregion

		#region LedDebug

		//【测试】发送 LedDebug 测试消息
		public ShareMessage sendLedDebugMessage(string value)
		{
			ShareMessage response = preTreatLedDebug(false, null);

			ShareMessage sendMsg = new ShareMessage();
			sendMsg.Type = MessageType.LedDebugMessage;
			sendMsg.Value = MessageValue.LED_Test;
			sendMsg.Parameters = new string[] { value };

			NamedPipeNative.SendMsg(ledDebugPipeName, sendMsg, out response, true);

			response = postTreat(response);

			return response;
		}

		//【命令】设置/取消设置有效的分控号、端口号
		public ShareMessage setEffectSubAndPort(string[] statusStrArray)
		{
			ShareMessage response = preTreatLedDebug(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.LedDebugMessage;
			commandMsg.Value = MessageValue.LED_SetEffectSubAndPort;
			commandMsg.Parameters = new string[statusStrArray.Length];
			Array.Copy(statusStrArray, commandMsg.Parameters, statusStrArray.Length);

			NamedPipeNative.SendMsg(ledDebugPipeName, commandMsg, out response, true);

			response = postTreat(response);

			return response;
		}

		//【查询】查询现有的分控个数
		public ShareMessage getSubControlNum()
		{
			ShareMessage response = preTreatLedDebug(false, null);

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.LedDebugMessage;
			commandMsg.Value = MessageValue.LED_GetSubControlNum;

			NamedPipeNative.SendMsg(ledDebugPipeName, commandMsg, out response, true);

			if (response == null)
			{
				response = new ShareMessage();
				response.Handled = true;
				response.Value = "error";
			}

			// 不执行命令后处理

			return response;
		}

		#endregion
	}
}
